/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>系统平台全局设置,包括基础设置、购物、短信、邮件、水印和分销等相关模块。</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>
    <el-form ref="form" :model="form" label-width="200px" label-position="right">
      <el-form-item label="商品图片添加水印">
        <el-switch v-model="form.is_mark" active-value="1" inactive-value="0"></el-switch>
        <p class="notic">商品图片添加水印</p>
      </el-form-item>
      <div v-if="form.is_mark == '1'">
        <el-form-item label="水印类型">
          <el-radio-group v-model="form.mark_type">
            <el-radio label="text">文字</el-radio>
            <el-radio label="img">图片</el-radio>
          </el-radio-group>
        </el-form-item>
        <div v-if="form.mark_type == 'text'">
          <el-form-item label="水印文字">
            <el-input v-model="form.mark_txt" size="mini"></el-input>
            <p class="notic">水印文字</p>
          </el-form-item>
          <el-form-item label="文字字号">
            <el-input v-model="form.mark_txt_size" size="mini"></el-input>
            <p class="notic">字体大小</p>
          </el-form-item>
          <el-form-item label="文字颜色">
            <el-color-picker v-model="form.mark_txt_color"></el-color-picker>
          </el-form-item>
        </div>
        <div v-if="form.mark_type == 'img'">
          <el-form-item label="水印图片">
            <el-upload
              class="avatar-uploader"
              :action="apiHead + '/mall/upload?type=water_mark'"
              :show-file-list="false"
              :on-success="handleWater"
              :before-upload="beforeAvatarUpload"
            >
              <el-image
                v-if="form.mark_img"
                :src="form.mark_img"
                style="width:90px; height:90px; display:block;"
              ></el-image>
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <p class="notic">用于商品缩略图上水印</p>
          </el-form-item>
          <el-form-item label="水印添加条件">
            <el-input v-model="form.mark_width" placeholder="图片宽度 单位像素(px)" size="mini"></el-input>
            <el-input v-model="form.mark_height" placeholder="图片高度 单位像素(px)" size="mini"></el-input>
            <p class="notic">水印的宽度和高度</p>
          </el-form-item>
          <el-form-item label="JPEG 水印质量">
            <el-slider v-model="form.mark_quality" style="width:400px"></el-slider>
            <p class="notic">水印质量请设置为0-100之间的数字,决定 jpg 格式图片的质量</p>
          </el-form-item>
        </div>
        <el-form-item label="水印透明度">
          <el-slider v-model="form.mark_degree" style="width:400px"></el-slider>
          <p class="notic">0代表完全透明，100代表不透明</p>
        </el-form-item>
        <el-form-item label="水印位置">
          <el-radio-group v-model="form.sel">
            <el-radio-button label="1">顶部居左</el-radio-button>
            <el-radio-button label="2">顶部居中</el-radio-button>
            <el-radio-button label="3">顶部居右</el-radio-button>
            <br />
            <el-radio-button label="4">中部居左</el-radio-button>
            <el-radio-button label="5">中部居中</el-radio-button>
            <el-radio-button label="6">中部居右</el-radio-button>
            <br />
            <el-radio-button label="7">底部居左</el-radio-button>
            <el-radio-button label="8">底部居中</el-radio-button>
            <el-radio-button label="9">底部居右</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </div>
      <el-form-item>
        <el-button type="primary" @click="onSubmit" :loading="subLoading">确认提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getConfigs, updateConfigs } from "@/utils/api";
export default {
  data() {
    return {
      form: {},
      subLoading: false
    };
  },
  components: {},
  created() {
    this.getConfigs();
  },
  methods: {
    getConfigs() {
      var that = this;
      getConfigs({ inc_type: "water" }).then(function(res) {
        that.form = res.water;
        that.form.mark_degree = parseInt(that.form.mark_degree);
        that.form.mark_quality = parseInt(that.form.mark_quality);
      });
    },
    onSubmit() {
      var that = this;
      that.subLoading = true;
      updateConfigs("water", that.form).then(function(res) {
        that.subLoading = false;
        if (res.status == 1) {
          that.$message.success("保存成功");
        } else {
          that.$message.error("保存失败,失败原因:" + res.msg);
        }
      });
    },
    handleWater(res) {
      if (res.status < 1) {
        this.$message.error(res.msg);
        return;
      }
      this.form.mark_img = res.result;
    },
    beforeAvatarUpload(file) {
      const isJPG =
        file.type === "image/gif" ||
        file.type === "image/jpg" ||
        file.type === "image/jpeg" ||
        file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传图片只能是 JPG、JPEG、PNG、GIF 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    }
  }
};
</script>
<style scoped>
.avatar-uploader-icon {
  line-height: 100px;
}
</style>
